﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QxFramework.Core;
public class ItemRigid : MonoBehaviour
{
    Dictionary<string, bool> use;//=new Dictionary<string, bool>();
    void DFS(GameObject obj,ref List<GameObject> list)
    {
        list.Add(obj);
        var joints = GameCombine.CombineManager.Get<GameCombine.IItemCombineManager>().ItemJointFields.FindAll(s => s.Item[0] == obj.name || s.Item[1] == obj.name);
        use[obj.name] = true;
        foreach(var x in joints)
        {
            var name = obj.name == x.Item[0] ? x.Item[1] : x.Item[0];
            if (use.ContainsKey(name))
            {
                continue;
            }
            var nxt = ClockManager.Get<IItemSyncManager>().GetGameObjectByName(name);
            if(nxt!=null&&nxt.transform.parent == this.transform)
            {
                DFS(nxt,ref list);
            }
        }
    }
    public void SetRigidInfo()
    {
        use = new Dictionary<string, bool>();
        List<GameObject> list=new List<GameObject>();
        if (this.transform.childCount == 0)
        {
            ObjectPool.Recycle(this);
            return;
        }
        DFS(this.transform.GetChild(0).gameObject, ref list);
        GameObject newrigid = null;
        if (list.Count != this.transform.childCount)
        {
            newrigid = GameControl.GameManager.Get<GameControl.IItemInstantiateManager>().CreateItemRigid();
        }
        float mass=0f;
        Dictionary<Transform, Vector3> posDic = new Dictionary<Transform, Vector3>();
        Vector3 sumPos = Vector3.zero;
  //      int cnt = 0;
       for(int i = this.transform.childCount - 1; i >= 0; i--)
        {
            if (list.Contains(this.transform.GetChild(i).gameObject)==false)
            {
                this.transform.GetChild(i).SetParent(newrigid.transform);
            }
            else
            {
                posDic[this.transform.GetChild(i)] = this.transform.GetChild(i).position;
                //          cnt++;

                sumPos += this.transform.GetChild(i).localPosition *( this.transform.GetChild(i).GetComponent<CombinableItem>().ItemFieldSetting.Mass-
                this.transform.GetChild(i).GetComponent<CombinableItem>().SetRigidInfo(this.GetComponent<Rigidbody2D>()));
                //          mass += this.transform.GetChild(i).GetComponent<CombinableItem>().ItemFieldSetting.Mass - this.transform.GetChild(i).GetComponent<CombinableItem>().SetRigidInfo(this.GetComponent<Rigidbody2D>()); ;
                //    this.transform.GetChild(i).transform.
            }
        }
        this.GetComponent<Rigidbody2D>().useAutoMass = true;
  //      this.GetComponent<Rigidbody2D>().centerOfMass = sumPos / this.GetComponent<Rigidbody2D>().mass;
     /*   var centerOfMass = this.GetComponent<Rigidbody2D>().centerOfMass;
        this.transform.position =this.transform.TransformPoint(this.GetComponent<Rigidbody2D>().centerOfMass);
        this.GetComponent<Rigidbody2D>().centerOfMass = Vector2.zero ;
        foreach(var x in posDic)
        {
            x.Key.position = x.Value;
        }*/
        if (newrigid)
        {
            newrigid.GetComponent<ItemRigid>().SetRigidInfo();

        }
    }
    public static void Bing(ItemRigid a,ItemRigid b)
    {
        Debug.Assert(a != b);
        while( b.transform.childCount>0)
        {
            b.transform.GetChild(0).SetParent(a.transform);
        }
        ObjectPool.Recycle(b.gameObject);
        a.SetRigidInfo();
    }
    public void LateUpdate()
    {
        if (this.transform.childCount == 0)
        {
            ObjectPool.Recycle(this.gameObject);
        }
    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawCube(this.GetComponent<Rigidbody2D>().worldCenterOfMass, Vector2.one * 0.1f);
    }
}
